package com.xiaobai.controller;

import com.alibaba.fastjson.JSON;
import com.xiaobai.common.CommonXQ;
import com.xiaobai.common.ServiceResponse;
import com.xiaobai.service.achievement.IAchievementService;
import com.xiaobai.units.HttpUnits;
import com.xiaobai.vo.AchievementItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/achievement")
public class AchievementController {

    @Autowired
    private IAchievementService achievementService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 在app内更新成绩(培育计划的!!!)
     * @param student_id
     * @param jsessionid
     * @return
     */
    @GetMapping("/selectNewAchievement")
    public ServiceResponse<List<AchievementItem>> selectNewAchievement(String student_id,String jsessionid){

        //1.需要判断我是否开启了查询成绩
        //2.开启了就继续,查出的培育计划新学期成绩存入数据库,并且传给微信前端,让其更新对应学期的成绩缓存
        //3.查出结果为空就返回一个不同的提示给微信就行

        /**
         * 前端检验,这步已经不需要了
         */
//        String flag=stringRedisTemplate.opsForValue().get("ac");//我通过设置redis的k-v来,然后这里判断V即可是否查询成绩
//
//        if(StringUtils.equals("0",flag)){
//            return ServiceResponse.createByErrorMsg("notime");
//        }

        String acxq= CommonXQ.CJB;//在redis中设置查询学期 例如:202001 202002

        Map<String,String> map=new HashMap<>();

        map.put("xnxqdm",acxq);
        map.put("page","1");
        map.put("rows","90");
        map.put("sort","zcj");
        map.put("order","desc");
        map.put("jhlxdm","01");

        String url="https://jwxt.gdupt.edu.cn/xskccjxx!getDataList.action";

        String result=HttpUnits.gyPost(url,jsessionid,map);

        if(result.length()<30){
            //当前学期成绩为空!!!
            return ServiceResponse.createByErrorMsg("nohave");
        }else{

            int start=result.indexOf("\"rows\":")+7;

            int end=result.length()-1;//正确

            result=result.substring(start,end);

            List<String> pyjhList = JSON.parseArray(result, String.class);

            List<AchievementItem> pyjhItemList=new ArrayList<>();

            for(int i=0;i<pyjhList.size();i++){
                //都加进itemList
                pyjhItemList.add(JSON.parseObject(pyjhList.get(i),AchievementItem.class));
            }

            //得到JSON,然后我们这里计算这是哪个学期的成绩单
            Integer gender=Integer.parseInt(student_id.substring(0,2));
            AchievementItem achievementItem=pyjhItemList.get(0);
            Integer ksyear=Integer.parseInt(achievementItem.getXnxqmc().substring(2,4));
            Integer ksxq=Integer.parseInt(achievementItem.getXnxqmc().substring(10,11));
            Integer xq=(ksyear-gender)*2+ksxq;//当前是第几学期的成绩单?

            String[] genders={"20"+gender+"01","20"+gender+"02","20"+(gender+1)+"01","20"+(gender+1)+"02","20"+(gender+2)+"01","20"+(gender+2)+"02","20"+(gender+3)+"01","20"+(gender+3)+"02"};
            // 明天5.11 测试一下  测试通过

            /**
             * 存在就更新，不存在就插入（必须现有唯一键  已有:student_id）
             */

            switch (xq){
                case 1:
                    List<AchievementItem> oneAchievementList=pyjhItemList;
                    achievementService.updateOrInsertAchievement(student_id,genders[xq-1],JSON.toJSONString(oneAchievementList));
                    return ServiceResponse.createBySuccess("1",oneAchievementList);
                case 2:
                    List<AchievementItem> twoAchievementList=pyjhItemList;
                    achievementService.updateOrInsertAchievement(student_id,genders[xq-1],JSON.toJSONString(twoAchievementList));
                    return ServiceResponse.createBySuccess("2",twoAchievementList);
                case 3:
                    List<AchievementItem> threeAchievementList=pyjhItemList;
                    achievementService.updateOrInsertAchievement(student_id,genders[xq-1],JSON.toJSONString(threeAchievementList));
                    return ServiceResponse.createBySuccess("3",threeAchievementList);
                case 4:
                    List<AchievementItem> fourAchievementList=pyjhItemList;
                    achievementService.updateOrInsertAchievement(student_id,genders[xq-1],JSON.toJSONString(fourAchievementList));
                    return ServiceResponse.createBySuccess("4",fourAchievementList);
                case 5:
                    List<AchievementItem> fiveAchievementList=pyjhItemList;
                    achievementService.updateOrInsertAchievement(student_id,genders[xq-1],JSON.toJSONString(fiveAchievementList));
                    return ServiceResponse.createBySuccess("5",fiveAchievementList);
                case 6:
                    List<AchievementItem> sixAchievementList=pyjhItemList;
                    achievementService.updateOrInsertAchievement(student_id,genders[xq-1],JSON.toJSONString(sixAchievementList));
                    return ServiceResponse.createBySuccess("6",sixAchievementList);
                case 7:
                    List<AchievementItem> sevenAchievementList=pyjhItemList;
                    achievementService.updateOrInsertAchievement(student_id,genders[xq-1],JSON.toJSONString(sevenAchievementList));
                    return ServiceResponse.createBySuccess("7",sevenAchievementList);
                case 8:
                    List<AchievementItem> eightAchievementList=pyjhItemList;
                    achievementService.updateOrInsertAchievement(student_id,genders[xq-1],JSON.toJSONString(eightAchievementList));
                    return ServiceResponse.createBySuccess("8",eightAchievementList);
            }
        }
        throw new RuntimeException("不该跑到这里的");
    }

    /**
     * 在app更新成绩(公选课的!!!)
     * @param student_id
     * @param jsessionid
     * @return
     */
    @GetMapping("/selectNewAcAndGxk")
    public ServiceResponse selectNewAcAndGxk(String student_id,String jsessionid){

        /**
         * 前端已经检验,这步已经不需要了
         */
//        String flag=stringRedisTemplate.opsForValue().get("ac");//我通过设置redis的k-v来,然后这里判断V即可是否查询成绩
//
//        if(StringUtils.equals("0",flag)){//redis中1是给查
//            return ServiceResponse.createByErrorMsg("notime");
//        }

        Map<String,String> map=new HashMap<>();

        map.put("page","1");
        map.put("rows","40");
        map.put("sort","zcj");
        map.put("order","desc");
        map.put("jhlxdm","02");//公选课

        String url="https://jwxt.gdupt.edu.cn/xskccjxx!getDataList.action";

        String result=HttpUnits.gyPost(url,jsessionid,map);

        if(result.length()<30){
            //当前学期课表为空!!!
            return ServiceResponse.createByErrorMsg("nohave");
        }else{
            int start=result.indexOf("\"rows\":")+7;

            int end=result.length()-1;//正确

            result=result.substring(start,end);

            result=result.replace("&mdash;","");

            List<String> gxkList = JSON.parseArray(result, String.class);

            List<AchievementItem> zeroAchievementList=new ArrayList<>();

            for(int i=0;i<gxkList.size();i++){
                //都加进itemList
                zeroAchievementList.add(JSON.parseObject(gxkList.get(i),AchievementItem.class));
            }

            achievementService.updateOrInsertAchievement(student_id,"00",JSON.toJSONString(zeroAchievementList));

            return ServiceResponse.createBySuccess("0",zeroAchievementList);
        }

    }

}
